<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" props="defaultProps" :expand-on-click-node="false">
        <!-- template中使用v-slot可以接受 { data, node }即结点的数据和结点本身 -->
        <template v-slot="{ data }">
          <el-row style="width: 100%" type="flex" justify="space-between">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event为默认的第一个参数，可以忽略,这里的$event表示点击事件，$event.command表示点击事件中的command -->
              <!-- 而data 表示点击事件中的data，即当前结点数据 -->
              <el-dropdown @command="operateDept($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">新增子部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <!-- .sync修饰符相当于update:showDialog -->
      <!-- 通过ref可以获取自定义组件实例对象，相当于在自定义组件中使用this -->
      <dept-edit ref="deptEdit" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDepartment="getDepartment" />
    </div>
  </div>
</template>

<script>
import { departmentGetService, departmentDeleteService } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import DeptEdit from './components/DeptEdit.vue'
export default {
  name: 'Department',
  components: {
    DeptEdit
  },
  data() {
    return {
      currentNodeId: 0,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDialog: false
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const res = await departmentGetService()
      // console.log(res)
      this.depts = transListToTreeData(res, 0)
      // console.log(this.depts)
    },
    operateDept(command, id) {
      // console.log(command)
      if (command === 'add') {
        this.showDialog = true
        this.currentNodeId = id
      } else if (command === 'edit') {
        this.showDialog = true
        this.currentNodeId = id
        // 注意，this.$refs.deptEdit.getDeptDetail是同步函数，要调用该函数
        // 需要nextTick，否则通过组件中的currentNodeId获取不到数据
        this.$nextTick(() => {
          this.$refs.deptEdit.getDeptDetail()
        })
        // this.$refs.deptEdit.getDeptDetail(id)
      } else if (command === 'del') {
        this.$confirm('您确定删除该部门吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await departmentDeleteService(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getDepartment()
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  margin-right: 20px;
}
</style>
